package DAO;


import hibernate.AssetChangeInfo;
import hibernate.AssetInfo;
import hibernateUtil.HibernateUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class ReportManagementDAO {

	public int count = 0;
	
	public List getAllMS(int start , int limit)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
				
		Query query = session.createQuery("from AssetMaintainInfo");
		count = query.list().size();
		query.setFirstResult(start);
		query.setMaxResults(limit);
		List l = query.list();
		trans.commit();		
		return l;
    }
	@SuppressWarnings("unchecked")
	public List getMSByQuery(int start,int limit,String assetNo,String name,
			String department,String level){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();	

		
		String str = "from AssetMaintainInfo where ";
		
		if(!"".equals(assetNo))
		{
			str = str + "assetNo=:assetNo ";
		}
		if(!"".equals(name))
		{
			if(!"".equals(assetNo))
			{
				str = str + "or name=:name ";
			}
			else
			{
				str = str + "name=:name ";
			}
		}
		if(!"".equals(department))
		{
			if(!"".equals(assetNo) || !"".equals(name))
			{
				str = str + "or department=:department ";
			}
			else
			{
				str = str + "department=:department ";
			}
		}
		if(!"".equals(level))
		{
			if(!"".equals(assetNo) || !"".equals(name) || !"".equals(department))
			{
				str = str + "or level=:level ";
			}
			else
			{
				str = str + "level=:level ";
			}
		}
		
		Query query = session.createQuery(str);    //查询数据库
		
		if(!"".equals(assetNo))
		{
			query.setString("assetNo", assetNo);
		}
		if(!"".equals(name))
		{
			query.setString("name", name);
		}
		if(!"".equals(department))
		{
			query.setString("useDepartment", department);
		}		
		if(!"".equals(level))
		{
			query.setString("level", level);
		}
		count = query.list().size();
		query.setFirstResult(start); 
		query.setMaxResults(limit); 
		List l = query.list();
		trans.commit();		
		return l;		
	}

    @SuppressWarnings("unchecked")    

	public List getAllAsset(int start , int limit)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();				
		Query query = session.createQuery("from AssetInfo");
		count = query.list().size();
		query.setFirstResult(start);
		query.setMaxResults(limit);
		List l = query.list();			
		trans.commit();		
		return l;
    }
    
    @SuppressWarnings("unchecked")

    public List getAssetByQuery(int start,int limit,String name,String type,String state,
    		                    String department,String startUseTime,String EndTime)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();	
		
		String str = "from AssetInfo where ";		
		if(!"".equals(name))
		{
			str = str + "name=:name ";
		}
		if(!"".equals(type))
		{
			if(!"".equals(name))
			{
				str = str + "or type=:type ";
			}
			else
			{
				str = str + "type=:type ";
			}
		}
		if(!"".equals(state))
		{
			if(!"".equals(name) || !"".equals(type))
			{
				str = str + "or state=:state ";
			}
			else
			{
				str = str + "state=:state ";
			}
		}
		if(!"".equals(department))
		{
			if(!"".equals(name) || !"".equals(type) || !"".equals(state))
			{
				str = str + "or department=:department ";
			}	
			else
			{
				str = str + "department=:department ";
			}			
		}		
		if((!"".equals(startUseTime)) && (!"".equals(EndTime)))
		{
			if(!"".equals(name) || !"".equals(type) || !"".equals(state) || !"".equals(department))
			{
				str = str + "or startUseTime between:startUseTime and:EndTime ";
			}	
			else
			{
				str = str + "startUseTime between:startUseTime and:EndTime ";
			}			
		}		
		Query query = session.createQuery(str);  
		
		if(!"".equals(name))
		{
			query.setString("name", name);
		}
		if(!"".equals(type))
		{
			query.setString("type", type);
		}
		if(!"".equals(state))
		{
			query.setString("state", state);
		}
		if(!"".equals(department))
		{
			query.setString("department", department);
		}		
		if(!"".equals(startUseTime))
		{
			Date startUseTime_1 = java.sql.Date.valueOf(startUseTime);
			query.setDate("startUseTime", startUseTime_1);
		}
		if(!"".equals(EndTime))
		{
			Date EndTime_1 = java.sql.Date.valueOf(EndTime);
			query.setDate("EndTime", EndTime_1);
		}	
		count = query.list().size();
		query.setFirstResult(start);  
		query.setMaxResults(limit); 	

		List l = query.list();
		trans.commit();
		
		return l;
    }

    @SuppressWarnings("unchecked")    

	public List getAllProvoke(int start , int limit)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
				
		Query query = session.createQuery("from AssetChangeInfo");
		count = query.list().size();
		query.setFirstResult(start);
		query.setMaxResults(limit);
		List l = query.list();
				
		trans.commit();		
		return l;
    }
    
    @SuppressWarnings("unchecked")

    public List getProvokeByQuery(int start,int limit,String assetNo,String status)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();	
		
		String str = "from AssetChangeInfo where ";		
		if(!"".equals(assetNo))
		{

			str = str + "assetNo=:assetNo ";

		}

		if(!"".equals(status))
		{
			if(!"".equals(assetNo))
			{
				str = str + "or status=:status ";
			}
			else
			{
				str = str + "status=:status ";
			}
		}	

		Query query = session.createQuery(str);  

		if(!"".equals(assetNo))
		{
			query.setString("assetNo", assetNo);
		}
		if(!"".equals(status))
		{
			query.setString("status", status);
		}				
		count = query.list().size();		
		query.setFirstResult(start);  
		query.setMaxResults(limit); 	

		List l = query.list();
		trans.commit();
		
		return l;
    }

    public List getProvokeByAsset(String assetNo){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		List l = session.createQuery("from AssetInfo where assetNo =:assetNo").setParameter("assetNo", assetNo).list();
 	    trans.commit();
		return l;
    }

    @SuppressWarnings("unchecked")    

	public List getAllDiscount(int start , int limit)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();				
		Query query = session.createQuery("from AssetInfo");
		count = query.list().size();
		query.setFirstResult(start);
		query.setMaxResults(limit);
		List l = query.list();			
		trans.commit();		
		return l;
    }
    
    @SuppressWarnings("unchecked")

    public List getDiscountByQuery(int start,int limit,String name,String type,String state,
    		                    String department)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();	
		
		String str = "from AssetInfo where ";		
		if(!"".equals(name))
		{
			str = str + "name=:name ";
		}
		if(!"".equals(type))
		{
			if(!"".equals(name))
			{
				str = str + "or type=:type ";
			}
			else
			{
				str = str + "type=:type ";
			}
		}
		if(!"".equals(state))
		{
			if(!"".equals(name) || !"".equals(type))
			{
				str = str + "or state=:state ";
			}
			else
			{
				str = str + "state=:state ";
			}
		}
		if(!"".equals(department))
		{
			if(!"".equals(name) || !"".equals(type) || !"".equals(state))
			{
				str = str + "or department=:department ";
			}	
			else
			{
				str = str + "department=:department ";
			}			
		}		
		
		Query query = session.createQuery(str);  
		
		if(!"".equals(name))
		{
			query.setString("name", name);
		}
		if(!"".equals(type))
		{
			query.setString("type", type);
		}
		if(!"".equals(state))
		{
			query.setString("state", state);
		}
		if(!"".equals(department))
		{
			query.setString("department", department);
		}		
	
		count = query.list().size();
		query.setFirstResult(start);  
		query.setMaxResults(limit); 	

		List l = query.list();
		trans.commit();		
		return l;
    }
}
